package com.bs.blogspace.service.impl;

import com.bs.blogspace.mapper.CommentMapper;
import com.bs.blogspace.model.Result;
import com.bs.blogspace.pojo.Comment;
import com.bs.blogspace.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Result<Comment> addComment(Comment comment) {
        Result<Comment> result = new Result<>();
        if (comment == null){
            result.setCode("414");
            result.setMessage("评论对象为空");
            return result;
        }
        Map<String,String> target = comment.getTarget();
        comment.setType(target.get("type"));
        comment.setCommentUserName(target.get("userName"));
        comment.setCommentTime(LocalDateTime.now().withNano(0));

        int count = commentMapper.addComment(comment);
        if (count < 0){
            result.setMessage("添加评论失败");
            result.setCode("414");
            return result;
        }
        result.setCode("200");
        result.setSuccess(true);
        result.setData(comment);
        result.setMessage("添加评论成功");
        return result;
    }

    @Override
    public Result deleteComment(String commentId) {
        Result result = new Result();
        if (StringUtils.isEmpty(commentId)){
            result.setMessage("传入的参数为null");
            result.setCode("414");
            return result;
        }
        int count = commentMapper.deleteComment(commentId);
        if (count < 0){
            result.setMessage("删除评论失败");
            result.setCode("414");
            return result;
        }
        result.setCode("200");
        result.setMessage("删除评论Id为：" + commentId + " 的评论成功");
        result.setSuccess(true);
        return result;
    }
}
